<template>
  <section class="ling-top-text improvement_personal">
    <h2>完善资料</h2>
    <div class="improvement_personal_detail" v-if="userDetail">
      <div class="improvement_personal_items">
        <van-field
          v-model="userDetail.name"
          clearable
          required
          label="姓名"
          placeholder="请填写姓名"
          name="username"
          v-validate="'required'"
          maxlength="10"
          :error="errors.has('username')"
        />
      </div>

      <div class="improvement_personal_items">
        <van-field
          v-model="userDetail.mobile"
          required
          clearable
          label="手机号"
          placeholder="请填写手机号"
          name="mobile"
          v-validate="'required|mobile'"
          maxlength="11"
          :error="errors.has('mobile')"
          :error-message="errors.first('mobile')"
        />
      </div>

      <div class="improvement_personal_items">
        <van-field
          v-model="userDetail.idNumber"
          required
          clearable
          label="身份证号"
          placeholder="请填写身份证号"
          name="isIdCard"
          v-validate="'required|isIdCard'"
          maxlength="18"
          :error="errors.has('isIdCard')"
          :error-message="errors.first('isIdCard')"
        />
      </div>

      <div class="improvement_personal_items">
        <van-field
          v-model="userDetail.emailAddress"
          required
          clearable
          label="邮箱"
          placeholder="请填写邮箱"
          name="email"
          v-validate="'required|email'"
          :error="errors.has('email')"
          :error-message="errors.first('email')"
        />
      </div>

      <div class="certificate_items" style="border-bottom: none; margin-top: 0">
        <div class="uploader ling-flex">
          <span>最高学历</span>
          <van-uploader :after-read="afterRead">
            <i>{{ userDetail.qualifications ? '修改' : '上传' }}</i>
          </van-uploader>
        </div>
      </div>
      <div
        class="improvement_personal_items"
        @click="show = true"
        style="margin-top: 0; padding: 0 17px; border:none"
        v-if="userDetail.qualifications"
      >
        <div class="certificate" :style="{ backgroundImage: `url(${userDetail.qualifications})` }"></div>
      </div>

      <div
        class="certificate_items"
        style="border-bottom: 0;margin-top:0; border-top: solid 1px #dddddd;"
      >
        <div class="uploader ling-flex">
          <span>技能证书</span>
          <van-uploader
            :after-read="uploadSkillsCertificate"
            multiple
            :max-count="6"
            placeholder="请上传图片"
            v-if="userDetail.skillsCertificateList"
          >
            <i>{{ userDetail.skillsCertificateList ? '修改' : '上传' }}</i>
          </van-uploader>
        </div>
      </div>
      <div
        class="improvement_personal_items"
        @click="show = true"
        style="margin-top: 0; padding: 0 0 0 17px"
      >
        <div
          v-for="item in userDetail.skillsCertificateList"
          :key="item.id"
          class="certificate"
          :style="{ backgroundImage: `url(${item})` }"
        ></div>
      </div>

      <div class="ling-linearButton improvement_btn" @click="handleSubmit">提交</div>
    </div>
  </section>
</template>

<script>
import { uploadToQiniu } from '@/utils/qiniuUpload'
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('myData')
export default {
  name: 'improvementPersonalPage',
  data() {
    return {
      isLeave: false,
      fileList: [],
    }
  },
  components: {},
  computed: mapState(['userDetail']),
  created() {},
  methods: {
    ...mapActions(['editMobileUser']),
    handleSubmit() {
      this.$validator.validateAll().then((result) => {
        if (result) {
          const { id, dataPerfection, mobile, ...otherProps } = this.userDetail
          this.$dialog
            .confirm({
              message: '是否确认提交？',
            })
            .then(() => {
              this.editMobileUser({ ...otherProps }).then((res) => {
                this.isLeave = true

                this.$router.go(-1)
              })
            })
        }
      })
    },
    afterRead(file) {
      uploadToQiniu(file.file, () => {})
        .then((response) => {
          this.$toast('上传成功')
          this.userDetail.qualifications = response
        })
        .catch((e) => {
          this.$toast('上传失败')
          console.log(e, 'catch')
        })
    },
    uploadSkillsCertificate(file) {
      let fileList = []
      if (file.constructor === Array) {
        fileList = file
      } else {
        fileList[0] = file
      }
      let skillsCertificateList = []
      fileList.map(async (item) => {
        await uploadToQiniu(item.file, () => {})
          .then((response) => {
            this.$toast('上传成功')
            skillsCertificateList.push(response)
            this.userDetail.skillsCertificateList = skillsCertificateList
          })
          .catch((e) => {
            this.$toast('上传失败')
            console.log(e, 'catch')
          })
      })
    },
  },
  // beforeRouteLeave(to, from, next) {
  //   // 导航离开该组件的对应路由时调用
  //   // 该页面不可返回上一个页面，但是可以通过触发handleSubmit方法返回
  //   if (from.path == '/improvementPersonal' && !this.isLeave) {
  //     next(false)
  //   } else {
  //     next()
  //   }
  // }
}
</script>

<style lang="less" scoped>
.improvement_personal {
  position: relative;
  .improvement_personal_detail {
    padding-left: 20px;
    position: relative;
    .improvement_personal_items,
    .certificate_items {
      border-bottom: solid 1px #dddddd;
      margin-top: 18px;
      position: relative;
      .van-cell {
        padding-top: 0;
      }
      .uploader {
        line-height: 45px;
        padding: 0 15px;
        input {
          width: 40px;
          height: 26px;
          opacity: 0;
          z-index: 1;
          position: absolute;
          right: 10px;
          overflow: hidden;
        }
        span {
          color: #646566;
          font-size: 14px;
        }

        i {
          font-size: 10px;
          color: #7ac5ef;
          font-style: normal;
          z-index: 0;
        }
      }
      .certificate {
        width: 95px;
        height: 60px;
        overflow: hidden;
        margin-right: 10px;
        margin-bottom: 10px;
        display: inline-block;
        background-size: cover;
        background-position: center center;
      }
    }
    .improvement_personal_items {
      span::before {
        content: '*';
        position: absolute;
        left: 7px;
        font-size: 14px;
        color: #f44;
      }
    }
  }
  .van-uploader {
    margin-right: 12px !important;
  }
}
</style>
